﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FluentUnity
{
    public static class ReflectionExtensions
    {
        public static TAttribute GetAttribute<TAttribute>(this Type type)
            where TAttribute : Attribute
        {
            Ensure.IsNotNull(() => type);
            
            return type.GetAttributes<TAttribute>().FirstOrDefault();
        }

        public static TAttribute GetAttribute<TAttribute>(this MemberInfo member)
            where TAttribute : Attribute
        {
            Ensure.IsNotNull(() => member);

            return member.GetAttributes<TAttribute>().FirstOrDefault();
        }

        public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this Type type)
            where TAttribute : Attribute
        {
            Ensure.IsNotNull(() => type);

            return type.GetCustomAttributes(typeof (TAttribute), true).Cast<TAttribute>();
        }

        public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this MemberInfo member)
            where TAttribute : Attribute
        {
            Ensure.IsNotNull(() => member);

            return member.GetCustomAttributes(typeof (TAttribute), true).Cast<TAttribute>();
        }
    }
}